/// <summary>
132
/// 清除颜色定义
133
/// </summary>
134
/// <param name="area"></param>
135
private void clearRuleColor(ICellArea area)
136
{
137
string startCell, endCell;
138
startCell = string.Format("{0}{1}", Common.CommonFunction.ColumnNumToColumnID(area.StartCol), area.StartRow);
139
endCell = string.Format("{0}{1}", Common.CommonFunction.ColumnNumToColumnID(area.EndCol), area.EndRow);
140
Excel.Range range = ExcelWorksheet.get_Range(startCell, endCell);
141
142
if (range != null)
143
{
144
range.Interior.ColorIndex = 0;
145
}
146
}
/// <summary>
149
/// 设置区域颜色
150
/// </summary>
151
/// <param name="area"></param>
152
/// <param name="color"></param>
153
private void setRuleColor(int startRow,int startCol,int endRow,int endCol, int color)
154
{
155
区域设置背景颜色方法 ----#region 区域设置背景颜色方法 ----
156
157
string startCell, endCell;
158
startCell = string.Format("{0}{1}", Common.CommonFunction.ColumnNumToColumnID(startCol), startRow);
159
endCell = string.Format("{0}{1}", Common.CommonFunction.ColumnNumToColumnID(endCol), endRow);
160
Excel.Range range = ExcelWorksheet.get_Range(startCell, endCell);
161
162
if (range != null)
163
{
164
range.Interior.Color = color;
165
166
///该方法设置区域颜色为无色
167
///range.Interior.ColorIndex = 0;
168
}
169
#endregion
170
171
177
}
/// <summary>
195
/// 返回Excel文档所有的Sheet
196
/// </summary>
197
/// <param name="excelPath"></param>
198
/// <returns></returns>
199
public List<string> getSheetFromExcel(string excelPath)
200
{
201
List<string> Sheets = new List<string>();
202
OleDbConnection Conn = new OleDbConnection();
203
Conn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + excelPath + ";Extended Properties=Excel 8.0;Persist Security Info=False";
204
Conn.Open();
205
System.Data.DataTable dt = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]
{ null, null, null, "TABLE" });
206
207
int count = dt.Rows.Count;
208
if (count == 0)
209
return null;
210
211
for (int i = 0; i < count; i++)
212
{
213
Sheets.Add(dt.Rows[i]["TABLE_NAME"].ToString());
214
}
215
return Sheets;
216
}
/// <summary>
219
/// 把Excel文档转换为Xml文档
220
/// </summary>
221
/// <param name="excelPath"></param>
222
/// <param name="xmlPath"></param>
223
public void changeExcelToXml(string excelPath, string xmlPath)
224
{
225
Object Nothing = System.Reflection.Missing.Value;
226
ApplicationClass excelApp;
227
Workbook excelBook;
228
excelApp = new ApplicationClass();
229
excelApp.Visible = false;
230
excelApp.DisplayAlerts = false;
231
excelApp.AlertBeforeOverwriting = false;
232
excelApp.AskToUpdateLinks = false;
233
//
234
excelBook = excelApp.Workbooks.Open(excelPath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
235
excelBook.SaveAs(xmlPath, XlFileFormat.xlXMLSpreadsheet, Nothing, Nothing, Nothing, Nothing, XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);
236
//close
237
excelBook.Close(Nothing, Nothing, Nothing);
238
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook);
239
excelBook = null;
240
241
excelApp.Application.Quit();
242
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
243
excelApp = null;
244
245
GC.Collect();
246
GC.WaitForPendingFinalizers();
247
}
248
/// <summary>
251
/// 把 Xml 格式的 Excel 文件转换成 二进制格式的 Excel 文件
252
/// </summary>
253
/// <param name="xlsPath"></param>
254
public void RepairXls(string xlsPath)
255
{
256
Workbook excelBook = null;
257
ApplicationClass excelApp = null;
258
object Nothing = System.Reflection.Missing.Value;
259
excelApp = new ApplicationClass();
260
excelApp.Visible = false;
261
excelApp.DisplayAlerts = false;
262
excelApp.AlertBeforeOverwriting = false;
263
excelApp.AskToUpdateLinks = false;
264
265
excelBook = excelApp.Workbooks.Open(xlsPath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
266
excelBook.SaveAs(xlsPath, XlFileFormat.xlExcel5, Nothing, Nothing, Nothing, Nothing, XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);
267
268
if (excelBook != null)
269
{
270
excelBook.Close(Nothing, Nothing, Nothing);
271
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook);
272
excelBook = null;
273
274
excelApp.Application.Quit();
275
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
276
excelApp = null;
277
}
278
GC.Collect();
279
GC.WaitForPendingFinalizers();
280
}
281
}
282
}