无法将某些列的 npoi excel 表设置为只读 我尝试过创建冻结 (sheet1.CreateFreezePane(0, 1, 0, 1);) 但不起作用
public void WriteExcelWithNPOI(DataTable dt, String extension,string MSANAme,string CheckValue)
{
IWorkbook workbook;
if (extension == "xlsx")
{
workbook = new XSSFWorkbook();
}
else if (extension == "xls")
{
workbook = new HSSFWorkbook();
}
else
{
throw new Exception("This format is not supported");
}
ISheet sheet1 = workbook.CreateSheet("Sheet 1");
//make a header row
IRow row1 = sheet1.CreateRow(0);
for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell = row1.CreateCell(j);
String columnName = dt.Columns[j].ToString();
cell.SetCellValue(columnName);
}
//loops through data
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row = sheet1.CreateRow(i + 1);
for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell = row.CreateCell(j);
String columnName = dt.Columns[j].ToString();
cell.SetCellValue(dt.Rows[i][columnName].ToString());
}
}
using (var exportData = new MemoryStream())
{
Response.Clear();
workbook.Write(exportData);
if (extension == "xlsx") //xlsx file format
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}" + MSANAme + CheckValue+ ".xlsx"));
Response.BinaryWrite(exportData.ToArray());
}
else if (extension == "xls") //xls file format
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename="+ MSANAme + CheckValue + ".xls"));
Response.BinaryWrite(exportData.GetBuffer());
}
Response.End();
}
}
所有Excel列都是可编辑的,需要将某些列设置为只读。请帮忙。 所有Excel列都是可编辑的,需要将某些列设置为只读。请帮忙。 所有Excel列都是可编辑的,需要将某些列设置为只读。请帮忙。
我将假设您的代码正在运行,因此我将其作为参考并对其进行编辑。
public void WriteExcelWithNPOI(DataTable dt, String extension,string MSANAme,string CheckValue)
{
IWorkbook workbook;
if (extension == "xlsx")
{
workbook = new XSSFWorkbook();
}
else if (extension == "xls")
{
workbook = new HSSFWorkbook();
}
else
{
throw new Exception("This format is not supported");
}
ISheet sheet1 = workbook.CreateSheet("Sheet 1");
//make a header row
IRow row1 = sheet1.CreateRow(0);
var cellStyleForLocked = workbook.CreateCellStyle(); //create the style in the beginning itself
cellStyleForLocked.IsLocked = true;
for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell = row1.CreateCell(j);
String columnName = dt.Columns[j].ToString();
cell.SetCellValue(columnName);
}
//loops through data
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row = sheet1.CreateRow(i + 1);
for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell = row.CreateCell(j);
String columnName = dt.Columns[j].ToString();
if(j < 6) //set the locked style only for first 6 columns.
cell.CellStyle = cellStyleForLocked;
cell.SetCellValue(dt.Rows[i][columnName].ToString());
}
}
using (var exportData = new MemoryStream())
{
Response.Clear();
workbook.Write(exportData);
if (extension == "xlsx") //xlsx file format
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}" + MSANAme + CheckValue+ ".xlsx"));
Response.BinaryWrite(exportData.ToArray());
}
else if (extension == "xls") //xls file format
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename="+ MSANAme + CheckValue + ".xls"));
Response.BinaryWrite(exportData.GetBuffer());
}
Response.End();
}
}
希望它对您有用!
VB.net 打击代码有效
Private Sub WriteExcelNPoi(ByVal 扩展为字符串) 将工作簿调暗为 IWorkbook Dim DT As DataTable = New DataTable() Dim strFilename As String = String.Empty
DT.Rows.Clear()
DT.Columns.Clear()
DT.Columns.Add("DANTAI_ID")
DT.Columns.Add("SYOKUIN_ID")
DT.Columns.Add("KENSYU_NO")
DT.Columns.Add("KAMOKU_HAN_NO")
DT.Columns.Add("SEIKYU_SAKI_KB")
DT.Columns.Add("BIKO_TX")
DT.Columns.Add("MAIL_ADDRESS_TX")
DT.Columns.Add("TEL_TX")
DT.Columns.Add("TODOHUKEN_NM")
DT.Columns.Add("BUMON_NM")
Try
Dim drow As DataRow
For i As Integer = 0 To 3
drow = DT.NewRow
drow("DANTAI_ID") = "00" & i.ToString
drow("SYOKUIN_ID") = "001" & i.ToString
drow("KENSYU_NO") = "001" & i.ToString
drow("KAMOKU_HAN_NO") = "001" & i.ToString
drow("SEIKYU_SAKI_KB") = "001" & i.ToString
drow("BIKO_TX") = "001" & i.ToString
drow("MAIL_ADDRESS_TX") = "001" & i.ToString
drow("TEL_TX") = "001" & i.ToString
drow("TODOHUKEN_NM") = "001" & i.ToString
drow("BUMON_NM") = "001" & i.ToString
DT.Rows.Add(drow)
Next
If extension = "xlsx" Then
workbook = New XSSFWorkbook()
strFilename = "SampleExport_" & Now().ToLongTimeString & ".xlsx"
ElseIf extension = "xls" Then
workbook = New HSSFWorkbook()
strFilename = "SampleExport_" & Now().ToLongTimeString & ".xls"
Else
Throw New Exception("This format is not supported")
End If
Dim sheet1 As ISheet = workbook.CreateSheet("Sheet 1")
Dim row1 As IRow = sheet1.CreateRow(0)
'cell style locked
Dim cellStyle = workbook.CreateCellStyle()
cellStyle.IsLocked = True
cellStyle.FillBackgroundColor = IndexedColors.Grey25Percent.Index
'cellStyle.FillPattern = FillPattern.SolidForeground
'cell style unlock
Dim unlockCellstyle = workbook.CreateCellStyle()
unlockCellstyle.IsLocked = False
'set header for excel
For j As Integer = 0 To DT.Columns.Count - 1
Dim cell As ICell = row1.CreateCell(j)
Dim columnName As String = DT.Columns(j).ToString()
cell.SetCellValue(columnName)
Next
'set value cell excel
For i As Integer = 0 To DT.Rows.Count - 1
Dim row As IRow = sheet1.CreateRow(i + 1)
For j As Integer = 0 To DT.Columns.Count - 1
Dim cell As ICell = row.CreateCell(j)
Dim columnName As String = DT.Columns(j).ToString()
If j < 6 Then 'lock range not edit data
cell.CellStyle = cellStyle
Else
cell.CellStyle = unlockCellstyle 'unlock range edit data
End If
cell.SetCellValue(DT.Rows(i)(columnName).ToString())
Next
Next
'Protect the sheet
sheet1.ProtectSheet("meosys")
'download file excel
Using exportData = New MemoryStream()
Response.Clear()
workbook.Write(exportData)
If extension = "xlsx" Then
Response.ContentType = "application/force-download"
Response.AddHeader("content-disposition", "attachment; filename=" & strFilename)
Response.BinaryWrite(exportData.ToArray())
ElseIf extension = "xls" Then
Response.ContentType = "application/force-download"
Response.AddHeader("content-disposition", "attachment; filename=" & strFilename)
Response.BinaryWrite(exportData.ToArray())
End If
exportData.Close()
Response.[End]()
End Using
Catch ex As Exception
Throw New Exception("This format is not supported")
End Try
End Sub
希望它对您有用!